package org.wyy.vm.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.wyy.vm.service.IUserService;
import org.wyy.vm.service.impl.UserServiceImpl;
import org.wyy.vm.utils.ImageScale;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

/**
 * @author w
 *
 */
public class Login extends JFrame {
	
	private IUserService userService = new UserServiceImpl();

	
	private static final long serialVersionUID = -1643818635745451265L;
	private JPanel contentPane;
	private JTextField nameField;
	private JPasswordField passwordField;

	/**
	 * Create the frame.
	 */
	public Login() {
		setTitle("登陆");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 406);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("用户名");
		lblNewLabel.setBounds(98, 189, 54, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("密  码");
		lblNewLabel_1.setBounds(98, 244, 54, 15);
		contentPane.add(lblNewLabel_1);
		
		nameField = new JTextField();
		nameField.setBounds(162, 186, 186, 21);
		contentPane.add(nameField);
		nameField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(162, 241, 186, 21);
		contentPane.add(passwordField);
		
		JButton btnNewButton = new JButton("注  册");
		btnNewButton.setBounds(71, 313, 93, 23);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				Reg reg = new Reg();
				reg.setVisible(true);
			}
		});
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("登  录");
		btnNewButton_1.setBounds(255, 313, 93, 23);
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String name = nameField.getText();
				String pwd = new String(passwordField.getPassword());
				
				if(name == null || "".equals(name.trim())) {
					JOptionPane.showMessageDialog(getThis(), "用户名格式错误", "提示信息", JOptionPane.WARNING_MESSAGE);
				}else if(pwd == null || "".equals(pwd.trim())) {
					JOptionPane.showMessageDialog(getThis(), "密码格式错误", "提示信息", JOptionPane.WARNING_MESSAGE);
				}else {
					boolean isLogin = userService.login(name, pwd);
					
					if(isLogin) {
						dispose();
						new VideoList().setVisible(true);
					}else {
						JOptionPane.showMessageDialog(getThis(), "用户名或密码不对", "提示信息", JOptionPane.WARNING_MESSAGE);
					}
				}
				
			}
		});
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setBounds(10, 10, 414, 152);
		ImageIcon icon = new ImageIcon(Login.class.getResource("/res/bkg.JPG"));
		lblNewLabel_2.setIcon(ImageScale.getImage(icon, lblNewLabel_2.getWidth(), lblNewLabel_2.getHeight()));
		contentPane.add(lblNewLabel_2);
	}
	
	JFrame getThis() {
		return this;
	}
}
